import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, setUserInfo, getUserName, setUserName } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: getUserName(),
    userInfo: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERINFO: (state, userinfo) => {
    state.userInfo = userinfo
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      // let user = {
      //   "pageNum": null,
      //   "pageSize": null,
      //   "userId": "6A39941ECE4247E799BD8587434ACE40",
      //   "userAccount": "hebei",
      //   "userName": "河北环境",
      //   "userPassword": null,
      //   "mobile": null,
      //   "phone": "13647311112",
      //   "position": "河北环境",
      //   "jobs": null,
      //   "age": 10,
      //   "sex": 1,
      //   "roleId": "631D32FD06454E65ADA5A891C9533029",
      //   "swjgDm": "11300000000",
      //   "swjgMc": "国家税务总局河北省税务局大企业税收服务和管理局（第一税务分局）",
      //   "status": 0,
      //   "isValid": 0,
      //   "createrId": "6A39941ECE4247E799BD8587434ACE39",
      //   "createrName": "系统管理员",
      //   "createTime": 1557022202000,
      //   "modifierId": "6A39941ECE4247E799BD8587434ACE39",
      //   "modifierName": "系统管理员",
      //   "modifyTime": 1620464464000,
      //   "deleterId": null,
      //   "deleterName": null,
      //   "deleteTime": null,
      //   "remarks": null,
      //   "roleName": "",
      //   "orgId": null,
      //   "cityMc": "国家税务总局河北省税务局",
      //   "cityDm": "11300000000",
      //   "isInitPassword": 0,
      //   "requestValue": null,
      //   "oldPassword": null,
      //   "imgId": null,
      //   "imgCode": null,
      //   "roleList": null,
      //   "realIp": "172.16.0.14",
      //   "allIp": "{\"x-forwarded-for\":\"127.0.0.1\"}",
      //   "sjjdbz": false
      // }
      // let token = '12e58a5d-cc3e-49a2-b81f-3ec69192a9e7'
      // commit('SET_TOKEN', token)
      // commit('SET_USERINFO', user)
      // setUserInfo(JSON.stringify(user))
      // commit('SET_NAME', user.swjgMc)

      // setUserName(user.swjgMc)
      // setToken(token)
      // resolve()


      login(userInfo).then(response => {
        const { data } = response
        console.log(response)
        commit('SET_TOKEN', response.token)
        commit('SET_USERINFO', data.user)
        console.log(data.user)
        setUserInfo(JSON.stringify(data.user))
        commit('SET_NAME', data.user.swjgMc)
        setUserName( data.user.swjgMc)
        setToken(response.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

